<!-- 仓库定义：蹇泽阳 -->
<template>
  <div class="app-container">
    <Find :OriginalFilter="{ static: true }" :formList="formList" @findClicked="findClicked" />
    <div class="mt10 flex">
      <el-card shadow="never" :body-style="{ padding: '5px 10px 5px' }" style="width: 100%">
        <h1 class="f16">仓库定义</h1>
        <Table
          :tableHeader="tableHeader"
          @selectList="selectTableList"
          :tableData="tableData"
          selection
          class="mt10"
          :isLoading="isLoading"
          @currentRow="currentRow"
        />
        <el-card shadow="never" :body-style="{ padding: '5px' }">
          <PageModule :PageData="{ total, buttonS: true }" @pageSizeChange="handleSizeChange" @pageChange="handleCurrentChange" />
        </el-card>
      </el-card>
    </div>

    <!-- 添加仓库 -->
    <el-dialog v-model="dialogVisible" title="添加仓库" width="50%">
      <div class="flex">
        <el-input class="mr10" placeholder="请输入仓库名称" v-model="warehouseName" />
        <el-input class="mr10" placeholder="请输入仓库类型" v-model="warehouseType" />
      </div>
      <el-button class="mt10" type="primary" @click="addWarehouse">添加库位</el-button>
      <div v-for="(item, index) in addressList" :key="index" class="flex">
        <el-input class="mt10" placeholder="请输入库位" v-model="item.address" />
        <el-input class="mt10" placeholder="请输入备注" v-model="item.remark" />
        <el-button class="mt10 ml10" type="primary" @click="removeWarehouse(index)">删除</el-button>
      </div>
      <el-button class="mt10" type="primary" @click="save">保存</el-button>
    </el-dialog>

    <!-- 修改仓库 -->
    <el-dialog v-model="dialogVisible2" title="修改仓库" width="50%">
      <div class="flex">
        <el-input class="mr10" placeholder="请输入仓库名称" v-model="warehouseName2" />
        <el-input class="mr10" placeholder="请输入仓库类型" v-model="warehouseType2" />
      </div>
      <el-button class="mt10" type="primary" @click="addWarehouse2">添加库位</el-button>
      <div v-for="(item, index) in addressList2" :key="index" class="flex">
        <el-input class="mt10" placeholder="请输入库位" v-model="item.address" />
        <el-input class="mt10" placeholder="请输入备注" v-model="item.remark" />
        <el-button class="mt10 ml10" type="primary" @click="removeWarehouse2(index)">删除</el-button>
      </div>
      <el-button class="mt10" type="primary" @click="save2">保存</el-button>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="Define">
import JMessage from '@/utils/JMessage'
import service from '@/utils/request'
import { debounce } from 'lodash'
import { watch } from 'vue'
import { onMounted } from 'vue'
import { ref } from 'vue'

const dialogVisible = ref(false)
const warehouseName = ref('')
const warehouseType = ref('')
const dialogVisible2 = ref(false)
const warehouseName2 = ref('')
const warehouseType2 = ref('')
const id2 = ref('')
const addressList2 = ref([
  {
    address: '',
    remark: '',
    stkId: ''
  }
])
const addressList = ref([
  {
    address: '',
    remark: ''
  }
])
const addWarehouse = () => {
  addressList.value.push({
    address: '',
    remark: ''
  })
}
const addWarehouse2 = () => {
  addressList2.value.push({
    address: '',
    remark: '',
    stkId: ''
  })
}

const removeWarehouse = (index: number) => {
  addressList.value.splice(index, 1)
}
const removeWarehouse2 = (index: number) => {
  addressList2.value.splice(index, 1)
}
const formList = ref([
  {
    type: 'input',
    inputPlaceholder: '请输入仓库名称',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'input',
    inputPlaceholder: '请输入仓库库位',
    inputValue: '',
    inputLabel: ''
  },
  {
    type: 'button',
    checkboxLabel: '置空',
    bgColor: '#1890FF'
  },
  {
    type: 'button',
    checkboxLabel: '查询'
  },
  {
    type: 'button',
    checkboxLabel: '删除打勾数据'
  },
  {
    type: 'button',
    checkboxLabel: '添加仓库'
  },
  {
    type: 'button',
    checkboxLabel: '修改仓库'
  }
])

// 表一模块
// 表一按钮
const findClicked = async (content: string) => {
  if (content === '置空') {
    formList.value.forEach((item: any) => {
      item.inputValue = ''
      item.selectValue = ''
      item.dateValue = ''
    })
  }
  if (content === '查询') {
    getTableList()
  }
  if (content === '删除打勾数据') {
    removeList()
  }
  if (content === '添加仓库') {
    dialogVisible.value = true
    warehouseName.value = ''
    warehouseType.value = ''
  }
  if (content === '修改仓库') {
    const res = await service.post<any, any>(`/store/warehouse/query?id=${currentTableRow.value.warehouseId}`)
    id2.value = res.data.id
    warehouseName2.value = res.data.warehouseName
    warehouseType2.value = res.data.warehouseType
    addressList2.value = res.data.address.map((item: any) => ({
      address: item.address,
      remark: item.remark,
      stkId: item.stkId
    }))
    // 获取当前行数据
    dialogVisible2.value = true
  }
}
const tableData = ref([])
const isLoading = ref(true)
// 分页器
const currentPage = ref(1)
const pageSize = ref(40)
const total = ref(0)
const handleSizeChange = (val: number) => {
  pageSize.value = val
  getTableList()
}
const handleCurrentChange = (val: number) => {
  currentPage.value = val
  getTableList()
}
const tableHeader = ref([
  {
    label: '仓库名称',
    prop: 'warehouseName'
  },
  {
    label: '仓库类型',
    prop: 'warehouseType'
  },
  {
    label: '仓库库位',
    prop: 'address'
  },
  {
    label: '备注',
    prop: 'remark'
  }
])
// 获取表数据
const getTableList = debounce(async () => {
  isLoading.value = true
  let tableDataList = await service.post<any, any>(
    `/store/warehouse/list?address=${formList.value.find((item: any) => item.inputPlaceholder === '请输入仓库库位')?.inputValue}&warehouseName=${
      formList.value.find((item: any) => item.inputPlaceholder === '请输入仓库名称')?.inputValue
    }&warehouseType=${formList.value.find((item: any) => item.inputPlaceholder === '请输入仓库类型')?.inputValue}`,
    {
      pageNum: currentPage.value,
      pageSize: pageSize.value
    }
  )
  isLoading.value = false
  tableData.value = tableDataList.data.records
  total.value = tableDataList.data.total
  currentRow()
}, 500)
onMounted(() => {
  getTableList()
})
watch(
  () =>
    formList.value.map((item: any) => {
      item.selectValue, item.inputValue, item.dateValue, item.checked
    }),
  () => {
    getTableList()
  }
)
// 点击表给另一张表传数据
const currentTableRow = ref({}) as any
const currentRow = async (row: any = tableData.value[0]) => {
  currentTableRow.value = row
}
const selectedLists = ref([]) as any
const selectTableList = (selectedList: any) => {
  selectedLists.value = selectedList
}
const save = async () => {
  if (addressList.value.some((item: any) => item.address === '')) {
    JMessage.error('请输入所有库位')
    return
  }
  const res = (await service.put(`/store/warehouse/save`, {
    warehouseName: warehouseName.value,
    warehouseType: warehouseType.value,
    address: addressList.value.map((item: any) => {
      return {
        warehouseName: warehouseName.value,
        address: item.address,
        warehouseType: warehouseType.value,
        remark: item.remark
      }
    })
  })) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success(res.msg)
    dialogVisible.value = false
    getTableList()
  } else {
    JMessage.error(res.msg)
  }
  addressList.value = [{ address: '', remark: '' }]
}
const save2 = async () => {
  if (addressList2.value.some((item: any) => item.address === '')) {
    JMessage.error('请输入所有库位')
    return
  }
  const res = (await service.put(`/store/warehouse/save`, {
    warehouseName: warehouseName2.value,
    warehouseType: warehouseType2.value,
    id: id2.value,
    address: addressList2.value.map((item: any) => {
      return {
        ...item,
        warehouseName: warehouseName2.value,
        warehouseType: warehouseType2.value
      }
    })
  })) as any
  // 判断请求是否成功
  if (res.code === 200) {
    JMessage.success(res.msg)
    dialogVisible2.value = false
    getTableList()
  } else {
    JMessage.error(res.msg)
  }
}
// 删除
const removeList = async () => {
  if (selectedLists.value.length !== 0) {
    // 向服务器发送请求，更新外派单信息
    const res = (await service.post(
      `/store/warehouse/delete?detailFlag=${0}`,
      selectedLists.value.map((item: any) => item.warehouseId)
    )) as any
    // 判断请求是否成功
    if (res.code === 200) {
      JMessage.success('删除成功')
      getTableList()
    } else {
      JMessage.error(res.msg)
    }
  }
}
</script>
